var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mime=require("mime")
// var bodyParser=require('body-parser')

var usersRouter = require('./controller/user');
var uploadRouter = require('./controller/upload');
var contentRouter = require('./controller/content');

var vertoken = require('./token/token')
var expressJwt = require('express-jwt')
var app = express();

var log = require('./logs/log')
log.use(app)

// 设置跨域
app.all('*', function (req, res, next) {
    console.log("request url:",req.url)
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    if(req.url.indexOf("/images") != -1){
        res.header("Content-Type", mime.getType(req.url));
    }else{
        res.header("Content-Type", "application/json;charset=utf-8");
    }
    res.header("Access-Control-Allow-Headers", "content-type,Authorization,X-Requested-With");
    next();
});

// 解析token获取用户信息
app.use(function (req, res, next) {
    var token = req.headers['authorization'];
    console.log("parsing token:",token)
    if (token === undefined) {
        return next();
    } else {
        vertoken.getToken(token).then((data) => {
            console.log("The user has the login")
            req.data = data;
            return next();
        }).catch((error) => {
            return next();
        })
    }
});

app.use(logger('dev'));
// app.use(bodyParser.json());// 添加json解析
// app.use(bodyParser.urlencoded({extended: false}));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());

// 设置托管静态目录; 项目根目录+ public.可直接访问public文件下的文件eg:http://localhost:3000/images/url.jpg
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', usersRouter);
app.use('/', uploadRouter);
app.use('/', contentRouter);

// 验证token是否过期并规定那些路由不需要验证
app.use(expressJwt({secret: 'unhejing', algorithms: ['HS256']}).unless({
    path: ['/login', '/register']
}))

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});

// token失效返回信息
app.use(function (err, req, res, next) {
    console.log("err:",err)
    if (err.status == 401) {
        console.log("token Invalid")
        return res.json({code: 1, message: 'token Invalid', status: 401})
    }
    return res.json({code: -1, message: 'system error'})
})
// error handler
app.use(function (err, req, res, next) { // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;
